<?php

	if( !isset($_COOKIE["username"]) ){
		echo "<script>window.location.href='login.php'</script>";
	}else{
		setcookie("username", $_COOKIE["username"],time() + 900);
	}

	include_once 'letter_pdf.php';

	
	
	if( !isset($_GET["patientNo"]) || preg_match("/^\d+$/", $_GET["patientNo"]) == false ){
		echo "<script>alert('No patient no find!');window.location.href='index.php';</script>";
	}
		
	$ini_array = parse_ini_file("config/config.ini");

	$fp = fopen("config/content.letter", "rw");
	$content = fread($fp, filesize("config/content.letter"));
	
	$conn = @mysqli_connect($ini_array["host"], $ini_array["username"], $ini_array["password"], $ini_array["db_name"]) or die("cann't connect the mysql db");
	mysqli_set_charset($conn, "utf8");
	ini_set('default_charset', "UTF-8");
	date_default_timezone_set($ini_array["default_timezone"]);
		
	$patientNo = mysqli_real_escape_string($conn,$_GET["patientNo"]);



	$getInfoSql = "select First_name, Surname, isFirstVisit, doctor_first_name, doctor_surname, doctor_title, doctor_address from patient left outer join doctor on patient.doctor_no = doctor.Doctor_no where patient.Patient_no=".$patientNo;
		
		
	
		
		
	$infoResult = mysqli_query($conn, $getInfoSql);
		
		
	$patientFirstName = "";
	$patientSurname = "";
	$doctorFirstName = "";
	$doctorSurname = "";
	$doctorTitle = "";
	$doctorAddress = "";
	$isFirstVisit = 0;

		
	if( $row = mysqli_fetch_array($infoResult) ){
		$patientFirstName = iconv('UTF-8', 'ISO-8859-1', $row["First_name"]);
		$patientSurname = iconv('UTF-8', 'ISO-8859-1', $row["Surname"]);
		$doctorFirstName = iconv('UTF-8', 'ISO-8859-1', $row["doctor_first_name"]);
		$doctorSurname = iconv('UTF-8', 'ISO-8859-1', $row["doctor_surname"]);
		$doctorTitle = iconv('UTF-8', 'ISO-8859-1', $row["doctor_title"]);
		$doctorAddress = iconv('UTF-8', 'ISO-8859-1', $row["doctor_address"]);
		$isFirstVisit = $row["isFirstVisit"];
	}else{
		echo "<script>alert('this patient no has no visit record!!!');window.location.href='index.php'</script>";
	}
	
	
	$fontStr = "Times";
	
	
	$pdf = new letter_pdf();
	$pdf->AddPage();
	
	
	
	$pdf->SetLineWidth("0.2");
	$pdf->SetDrawColor(0, 0, 0);
	$pdf->Rect(8, 10, 190, 30, "D");
	$pdf->Rect(9, 11, 80, 28, "D");
	$pdf->Rect(91, 11, 106, 28, "D");
	
	$pdf->SetFont($fontStr, "B");
	$pdf->SetFontSize(16);
	$pdf->Text(21, 20, "Dr. Geraldine Morrow");
	$pdf->Text(18, 26, "Consultant Dermatologist");
	$pdf->SetFont($fontStr);
	$pdf->SetFontSize(8);
	$pdf->Text(24, 31, "M.B., B.Ch., B.A.O., M.R.C.P.I.");
	$pdf->Text(22, 38, "Medical Council  Reg. No. 009981");
	
	$pdf->SetFont($fontStr, "BI");
	$pdf->SetFontSize(10);
	$pdf->Text(93, 20, "All Correspondence to: ");
	$pdf->Text(93, 24, "Phone : ");
	$pdf->Text(93, 28, "e-mail: ");
	
	$pdf->SetFont($fontStr, "");
	$pdf->Text(135, 20, "39 Grosvenor Road,  Rathmines,  D6");
	$pdf->Text(135, 24, "01-4970392          Fax.:  01-4974119");
	$pdf->SetFont($fontStr, "U");
	$pdf->Text(135, 28, "dermatology.secretary@gmail.com");
	
	$pdf->setFont($fontStr, "B");
	$pdf->Text(105, 38, "Practice Website :");
	
	$pdf->setFont($fontStr, "U");
	$pdf->Text(140, 38, "www.geraldinemorrow.com");
	
	$pdf->setFont($fontStr, "");
	$pdf->Text(27, 60, "To:".$doctorTitle." ".$doctorFirstName." ".$doctorSurname.",");

	$pdf->Text(149, 60, "Ref No.:".$patientNo);

	
	
	
	$pdf->SetY(56);
	
	$pdf->Text(149, $pdf->GetY() + 8, $patientFirstName." ".$patientSurname);


	$addressSplit = explode(',', $doctorAddress);
	$length = count($addressSplit);
	for($i = 0; $i < $length; $i++ ){
		$pdf->SetXY(32, $pdf->GetY() + 6);
		if( $i < 4 ){
			if( $i == ($length - 1) ){
				$pdf->Cell(65, 6, trim($addressSplit[$i]));
			}else{
				$pdf->Cell(65, 6, trim($addressSplit[$i].","));
			}
		}else{
			break;
		}
		
	}
	

	$pdf->SetY(90);
	$pdf->Text(22, $pdf->GetY(), "Re:I reviewed ".$patientFirstName." ".$patientSurname);
	$pdf->Text(149, $pdf->GetY(), date("d-m-Y"));

	$pdf->SetY($pdf->GetY() + 12);
	$pdf->Text(22, $pdf->GetY(), "Dear ".$doctorTitle." ".$doctorFirstName);
	
	
	//$pdf->Line(22, 272, 185, 272);
	$pdf->SetLeftMargin(22);
	$pdf->SetRightMargin(20);
	$pdf->SetXY(30, $pdf->GetY());
	$pdf->Write(5, $content);
	
	
	
	

	
	
	$pdf->Output("letter_to_clinic.pdf", "I");
?>

